// https://juejin.cn/post/7371716384847364147

/**
 * https://juejin.cn/post/7371716384847364147
 *尝试用' tabularData '的属性列(或使用' properties ')和' tabularData '的行构建一个表，并记录它。如果不能将参数解析为表格，则退回到仅记录参数。
 *
 * ```js
 * // These can't be parsed as tabular data
 * console.table(Symbol());
 * // Symbol()
 *
 * console.table(undefined);
 * // undefined
 *
 * console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);
 * // ┌─────────┬─────┬─────┐
 * // │ (index) │  a  │  b  │
 * // ├─────────┼─────┼─────┤
 * // │    0    │  1  │ 'Y' │
 * // │    1    │ 'Z' │  2  │
 * // └─────────┴─────┴─────┘
 *
 * console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);
 * // ┌─────────┬─────┐
 * // │ (index) │  a  │
 * // ├─────────┼─────┤
 * // │    0    │  1  │
 * // │    1    │ 'Z' │
 * // └─────────┴─────┘
 * ```
 * @since v10.0.0
 * @param properties Alternate properties for constructing the table.
 */
function table(data: Object | Array<Object>, properties?: readonly string[]) {
  if (import.meta.env.DEV) {
    console.table(data, properties)
  }
}

// 美化打印实现方法
// 美化打印实现方法
const prettyLog = () => {
  const isEmpty = (value: any) => {
    if (import.meta.env.PROD) {
      return
    }
    return value == null || value === undefined || value === ''
  }
  const prettyPrint = (title: string, text: string, color: string) => {
    if (import.meta.env.PROD) {
      return
    }
    console.log(
      `%c ${title} %c ${text} %c`,
      `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
      `border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,
      'background:transparent'
    )
  }
  const info = (textOrTitle: string, content = '') => {
    if (import.meta.env.PROD) {
      return
    }
    const title = isEmpty(content) ? 'Info' : textOrTitle
    const text = isEmpty(content) ? textOrTitle : content
    prettyPrint(title, text, '#909399')
  }
  const error = (textOrTitle: string, content = '') => {
    if (import.meta.env.PROD) {
      return
    }
    const title = isEmpty(content) ? 'Error' : textOrTitle
    const text = isEmpty(content) ? textOrTitle : content
    prettyPrint(title, text, '#F56C6C')
  }
  const warning = (textOrTitle: string, content = '') => {
    if (import.meta.env.PROD) {
      return
    }
    const title = isEmpty(content) ? 'Warning' : textOrTitle
    const text = isEmpty(content) ? textOrTitle : content
    prettyPrint(title, text, '#E6A23C')
  }
  const success = (textOrTitle: string, content = '') => {
    if (import.meta.env.PROD) {
      return
    }
    const title = isEmpty(content) ? 'Success ' : textOrTitle
    const text = isEmpty(content) ? textOrTitle : content
    prettyPrint(title, text, '#67C23A')
  }

  const colors = [
    '#FF5733',
    '#33FF57',
    '#3357FF',
    '#FF33A1',
    '#A133FF',
    '#33FFF5',

    '#FF3333',
    '#33FF33',
    '#3333FF',
    '#FF8C33',
    '#33FF8C',
    '#8C33FF',
    '#FF338C',
    '#338CFF',
    '#FF338C',
    '#33FFCE',
    '#CE33FF',
    '#FFCE33',
    '#33CEFF',
    '#FF5733',
    '#33FF57',
    '#3357FF',
    '#FF33A1',
    '#A133FF',
    '#33FFF5',
    '#FF3333',
    '#33FF33',
    '#3333FF',
    '#FF8C33',
    '#33FF8C',
    '#8C33FF',
    '#FF338C',
    '#338CFF',
    '#FF338C',
    '#33FFCE',
    '#CE33FF',
    '#FFCE33',
    '#33CEFF',
    '#FF5733',
    '#33FF57',
    '#3357FF',
    '#FF33A1',
    '#A133FF',
    '#33FFF5',
    '#FF3333',
    '#33FF33',
    '#3333FF',
    '#FF8C33',
    '#33FF8C',
    '#8C33FF',
    '#FF338C',
    '#338CFF',
    '#FF338C',
    '#33FFCE',
    '#CE33FF',
    '#FFCE33',
    '#33CEFF',
    '#FF5733',
    '#33FF57',
    '#3357FF',
    '#FF33A1',
    '#A133FF',
    '#33FFF5',
    '#0052d9',
    '#FF3333',
    '#33FF33',
    '#3333FF',
    '#FF8C33',
    '#33FF8C',
    '#8C33FF',
    '#FF338C',
    '#338CFF',
    '#FF338C',
    '#33FFCE',
    '#CE33FF',
    '#FFCE33',
    '#33CEFF',
    '#FF5733',
    '#33FF57',
    '#3357FF',
    '#FF33A1',
    '#A133FF',
    '#33FFF5',
    '#366ef4',
    '#FF3333',
    '#33FF33',
    '#3333FF',
    '#FF8C33',
    '#33FF8C',
    '#8C33FF',
    '#FF338C',
    '#338CFF',
    '#6a9955'
  ]

  const random = (text: string, ...args: any[]) => {
    const color = colors[Math.floor(Math.random() * colors.length)]
    const styleLabel = `
    background: ${color};
    border: 1px solid ${color};
    padding: 1px;
    border-radius: 2px 0 0 2px;
    color: #fff;
  `
    const styleReset = 'background: transparent'
    // 使用 %c 格式化日志样式
    console.trace(`%c ${text} %c`, styleLabel, styleReset, ...args)
  }

  const log = (text: string, ...args: any[]) => {
    const color = colors[Math.floor(Math.random() * colors.length)]
    const styleLabel = `
    background: ${color};
    border: 1px solid ${color};
    padding: 1px;
    border-radius: 2px 0 0 2px;
    color: #fff;
  `
    const styleReset = 'background: transparent'
    // 使用 %c 格式化日志样式
    console.log(`%c ${text} %c`, styleLabel, styleReset, ...args)
  }

  // retu;
  return {
    info,
    error,
    warning,
    success,
    random,
    log: log
  }
}

const { info, error, warning, success, random, log } = prettyLog()

export default { table, info, error, warning, success, random, log }
